package com.ruike.alisurface.services;

import android.app.IntentService;
import android.content.Intent;
import android.text.TextUtils;

import com.ruike.alisurface.Constant;
import com.ruike.alisurface.MyApplication;
import com.ruike.alisurface.Serials.MsgEventBus;
import com.ruike.alisurface.http.MchApiHttp;
import com.ruike.alisurface.utils.ShareKey;
import com.voodoo.lib_frame.base.BaseView;
import com.voodoo.lib_frame.httpTools.DownloadUtil;
import com.voodoo.lib_utils.L;
import com.voodoo.lib_utils.ShareUtils;

import org.greenrobot.eventbus.EventBus;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;

/**
 * 图片下载
 * 根据接口返回信息  解析图片路径进行下载 判断是否存在
 */
public class MyImageService extends IntentService implements BaseView {

    public MyImageService() {
        super("MyImageService");
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }


    @Override
    protected void onHandleIntent(Intent intent) {
        try {
            L.i("下载图片--MyImageService");
            if (ShareUtils.getInstance().getInt(ShareKey.IS_SHOP_TYPE, 1) == 1) {
                MchApiHttp.getSlots(this);    //  货道展示商品
            } else {
                MchApiHttp.getSlotsType(this); //  商品类型列表数据
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    @Override
    public void onStarted(String loadingStr) {

    }

    @Override
    public void onLoading(long total, long current) {

    }

    @Override
    public void onComplete() {

    }

    @Override
    public void onErrorTip(String errorStr) {

    }

    @Override
    public void onSuccess(String requestUrl, String requestJsonStr) {
        JSONObject object = null;
        try {
            object = new JSONObject(requestJsonStr);
            String data = object.optString("data");
            if (requestUrl.endsWith("getSlots") || requestUrl.endsWith("getProduct")) { // 获取商品图片信息
                EventBus.getDefault().post(new MsgEventBus("商品数据", data));
                // 暂无需处理上报后回传的信息
                JSONArray jsonArray = new JSONArray(data);
                ArrayList<String> imglist = new ArrayList<>();
                if (jsonArray.length() > 0) {
                    for (int i = 0; i < jsonArray.length() - 1; i++) {
                        JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                        String productimg = jsonObject.optString("productimg");
                        if (TextUtils.isEmpty(productimg) || "null".equals(productimg)) {
                            continue;
                        }
                        imglist.add(productimg);
                    }
                    if (imglist.size() > 0) {
                        L.i("下载图片-- 图片数量--", imglist.size());
                        Intent intent = new Intent(MyApplication.getAppContext(), MyShopImgService.class);
                        intent.putStringArrayListExtra("ShopImglist",imglist);
                        startService(intent);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(String requestUrl, String requestMsg) {
        L.i("商品信息--onFailure ", requestMsg);
    }

    @Override
    public void onError(String requestUrl, String errorMsg) {
        L.i("商品信息--onError ", errorMsg);
        EventBus.getDefault().post(new MsgEventBus("商品数据异常", errorMsg));
    }

    @Override
    public void onError(String requestUrl, String errorMsg, String data) {

    }

    @Override
    public void onException(Exception exception) {

    }

    @Override
    public void onDownloadSuccess(String requestUrl, File file) {
        L.i("下载图片--onDownloadSuccess ", file.getAbsoluteFile());

    }

    @Override
    public void onDownloading(String requestUrl, long total, long progress) {

    }

    @Override
    public void onDownloadFailed(String requestUrl, Exception e) {
        L.i("下载图片--onDownloadFailed ", e.toString());
    }
}